package cn.yh.springbootjwt.service.impl;

import cn.yh.springbootjwt.domain.entity.ShoppingCartDo;
import cn.yh.springbootjwt.domain.query.ShoppingCartUpdate;
import cn.yh.springbootjwt.domain.vo.ShoppingCartListVo;
import cn.yh.springbootjwt.mapper.ShoppingCartMapper;
import cn.yh.springbootjwt.service.IShoppingCartService;
import cn.yh.springbootjwt.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public Integer addShoppingCart(Long productId) {
        Long userId = JWTUtils.getUserIdByClaim();
        ShoppingCartDo shoppingCart = shoppingCartMapper.isExist(productId,userId);
        if (shoppingCart != null){
            return shoppingCartMapper.updateShoppingCartNum(productId, userId);
        }
        return shoppingCartMapper.insertShoppingCartNum(productId, userId);
    }

    @Override
    public List<ShoppingCartListVo> selectShoppingCartList() {
//        System.out.println("UserId = " + JWTUtils.getUserIdByClaim());
//        System.out.println(shoppingCartMapper.selectShoppingCartList(JWTUtils.getUserIdByClaim()));
        return shoppingCartMapper.selectShoppingCartList(JWTUtils.getUserIdByClaim());
    }

    @Override
    public int updateShoppingCart(ShoppingCartUpdate shoppingCartUpdate) {
        return shoppingCartMapper.updateShoppingCart(shoppingCartUpdate.getNum(),shoppingCartUpdate.getShoppingCartId());
    }

    @Override
    public int deleteShoppingCart(Long shoppingCartId) {
        return shoppingCartMapper.deleteShoppingCart(shoppingCartId);
    }

}


